<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="__STATIC_URL__/static/mobile/css/weui.css" />
    <link rel="stylesheet" href="/address.css" />
</head>
<body>
<div class="container" id="container">
    <div class="page panel js_show">

        <div class="page__bd">
            <div class="weui-cells weui-cells_form" style="margin-top: 15px;">
                <form action="" method="post" id="forminfo">
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">收件人</label></div>
                        <div class="weui-cell__bd">
                            <input type="hidden" id="openid" value="{$info['openid']}">
                            <input class="weui-input" type="text" name="name" id="name" placeholder="输入收件人" value="{$info['name']}">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" name="phone" id="phone" placeholder="输入联系电话"  value="{$info['phone']}">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">所在地区</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" name="area" id="J_Address" placeholder="请选择收货地址" value="{$info['area']}">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
                        <div class="weui-cell__bd">
                            <input type="hidden" id="card_id" value="{$info['card_id']}" >
                            <input type="hidden" id="code" value="{$info['code']}">

                            <input class="weui-input" type="text" name="address" id="address" placeholder="请输入街道地址" value="{$info['address']}">
                        </div>
                    </div>

                </form>
            </div>
            <div class="mgt15 saveinfo" style="margin:20px;">
                <a href="javascript:;" class="weui-btn weui-btn_primary saveinfo" data-id="" >添加地址</a>
            </div>
        </div>
    </div>
</div>
<!--toast-->
<div id="toast" style="opacity: 1; display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content">已完成</p>
    </div>
</div>
</body>
<script src="__STATIC_URL__/static/jquery/3.2.1/jquery.min.js"></script>
<script src="/city.js"></script>
<script src="__STATIC_URL__/static/layer/layer.js" type="text/javascript" charset="utf-8"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

<script src="/address.js"></script>
<script>
    wx.config(
    {$jssdk['wxjssdk']}
    );
    wx.ready(function () {
    wx.openAddress({
        success: function (res) {
            console.log(132,res);

            var userName = res.userName; // 收货人姓名
            $("#name").val(userName);
            var postalCode = res.postalCode; // 邮编
            var provinceName = res.provinceName; // 国标收货地址第一级地址（省）
            var cityName = res.cityName; // 国标收货地址第二级地址（市）
            var countryName = res.countryName; // 国标收货地址第三级地址（国家）
            $("#J_Address").val(provinceName+cityName+countryName);
            var detailInfo = res.detailInfo; // 详细收货地址信息
            var nationalCode = res.nationalCode; // 收货地址国家码
            var telNumber = res.telNumber; // 收货人手机号码
            $("#phone").val(telNumber);
            $("#address").val(detailInfo);
        },
        cancel: function (errMsg) {
         // 用户取消拉出地址
         // alert(errMsg);
        }
    });
})

</script>
<script>
    !function() {
        var $target = $('#J_Address');

        $target.citySelect();

        $target.on('click', function(event) {
            event.stopPropagation();
            $target.citySelect('open');
        });

        $target.on('done.ydui.cityselect', function(ret) {
            $(this).val(ret.provance + ' ' + ret.city + ' ' + ret.area);
        });
    }();
</script>

<script type="text/javascript">
    $(function () {
        $('.saveinfo').click(function (ev) {
            var name = $('#name').val();
            var phone = $('#phone').val();
            var address = $('#address').val();
            var J_Address = $("#J_Address").val();
            var card_id = $("#card_id").val();
            var code = $("#code").val();
            if (name == '') {
                layer.msg('收件人不能为空');
                return false;
            }
            if (phone == '') {
                layer.msg('联系电话不能为空');
                return false;
            }
            if (J_Address == '') {
                layer.msg('地区不能为空');
                return false;
            }
            if (address == '') {
                layer.msg('详细地址不能为空');
                return false;
            }
            $.ajax({
                url:"{:url('cardgifts/edituserinfo')}",
                type:'post',
                data:{
                    name:name,
                    phone:phone,
                    address:address,
                    area:J_Address,
                    card_id:card_id,
                    code:code
                },
                success:function(data){
                    // console.log(13213,data)
                    if(data.code==0){
                        var $toast=$("#toast");
                        var tip = $(".weui-toast__content");
                        tip.text(data.info);
                        $toast.fadeIn(100);
                        setTimeout(function () {
                            $toast.fadeOut(100);
                        }, 1000);
                        setTimeout(function(){
                            window.location.href=data.url;
                        },1500)

                    }
                }
            })
        })
    })
</script>
</html>
